package com.netstar.salesman_90dsp;

import roboguice.activity.RoboActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.netstar.salesman_90dsp.tool.DateChooseDialog;
import com.netstar.salesman_90dsp.tool.DialogTool;
import com.netstar.salesman_90dsp.tool.WaittingDialog;

/**
 * activity基类
 * 
 * @author zhangshuai
 * 
 */
public abstract class BaseActivity extends RoboActivity implements
		BaseActivityInterface {
	/**
	 * 应用上下文
	 */
	public Context activityContext;
	/**
	 * view 点击监听器
	 */
	protected OnViewClicklistener onViewClicklistener = new OnViewClicklistener();

	/**
	 * 弹出框工具
	 */
	public DialogTool dialogTool;
	/**
	 * 等待框
	 */
	public WaittingDialog waittingAlertDialog;
	/**
	 * 时间选择框
	 */
	public DateChooseDialog dateChooseDialog;
	/**
	 * 网络控制器
	 */
	public AsyncHttpClient asyncHttpClient;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		subInitContentView(savedInstanceState);

		initContentView(savedInstanceState);
		initView();
		initParam();
		setOnclick();
		runSomeThing();

		subInitView();
		subInitParam();
		subSetOnclick();
		subRunSomeThing();
	}

	@Override
	public void initContentView(Bundle savedInstanceState) {

	}

	@Override
	public void initView() {
	}

	@Override
	public void initParam() {
		activityContext = this;
		asyncHttpClient = ((AppContext) getApplication()).asyncHttpClient;
		dialogTool = new DialogTool(this);
	}

	@Override
	public void setOnclick() {
	}

	@Override
	public void runSomeThing() {

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}

	/**
	 * 设置默认的点击监听器
	 * 
	 * @param view
	 */
	public void setOnclickWithDefaultListener(View view) {
		view.setOnClickListener(onViewClicklistener);
	}

	/**
	 * 默认点击事件的回调
	 */
	public void callbackWithDefaultListener(View view) {

	}

	/**
	 * 监听器类
	 * 
	 * @author zhangshuai
	 * 
	 */
	private class OnViewClicklistener implements OnClickListener {

		@Override
		public void onClick(View v) {
			callbackWithDefaultListener(v);
		}
	}

	/**
	 * 获取 textview 字符串
	 * 
	 * @param textView
	 * @return
	 */
	public static String getStringFromView(TextView textView) {
		String viewStr = null;
		try {
			viewStr = textView.getText().toString();
		} catch (Exception e) {
			viewStr = null;
		}
		return viewStr;
	}

	/**
	 * 获取 editText 字符串
	 * 
	 * @param editText
	 * @return
	 */
	public static String getStringFromView(EditText editText) {
		String viewStr = null;
		try {
			viewStr = editText.getText().toString();
		} catch (Exception e) {
			viewStr = null;
		}
		return viewStr;
	}

	/**
	 * 获取异常中的信息
	 * 
	 * @param throwable
	 * @return
	 */
	public static String getStringFromThrowable(Throwable throwable) {
		String throwableStr = null;
		try {
			throwableStr = throwable != null && throwable.getCause() != null ? throwable
					.getCause().getMessage() : null;
		} catch (Exception e) {
			throwableStr = null;
		}
		return throwableStr;
	}
}
